//Do-fil to create predicted margins plots
// Figure 2.
// Last changed: 2020-10-04 (KOL)
  
  clear all
  set more off 
  set matsize 10000
  
  capture log close
  local output "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy"
  
  use "E:\ProjData\PlacementCandidacy\utp-DataMainSample.dta", clear
  
//####################################### TABLE 1 #################################// 
  local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1"
  local controls1 "i.InvAr i.IFAUkod i.Ar i.FodelseAr"
  local controls2 "Kvinna Gift_InvAr UtbAr_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
   
  listwise Alder Kvinna Gift_InvAr Barn0_15_InvAr UtbAr_InvAr logpEnklavInvAr Nominerad Vald  InvAlder  InvAr TidI InvAlder logpEnklavVuxenInvAr
  
//IV-estimates
	xtivreg Nominerad (logpEnklavVuxen=logpEnklavVuxenInvAr) `controls1' `controls2' if `sample', ///
			i(InKommun) vce(cluster InKommun) fe first
	*sum Nominerad if e(sample)==1		

//Vary enclave size from 10th percentile to 90th percentile, averaging over all other variables
	margins, at(logpEnklav=(-8.5(.02)-3))
	
	gen b = .
	gen se = .
	gen logden = .
	mat B = r(b)
	mat V = r(V)
	matrix A = r(at)
	matrix A = A[.,"logpEnklavVuxen"]
	
	foreach num of numlist 1/280 {
	replace b = B[1, `num'] in `num'
	replace se = sqrt(V[`num', `num']) in `num'
	replace logden = A[`num', 1] in `num'
	}
	
	gen lci = b-1.96*se
	gen hci = b+1.96*se
	gen pden = exp(logden)
	keep in 1/280
	keep b lci hci pden logden
	
	save "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy\Output\predmarg.dta", replace
	
	use "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy\Output\predmarg.dta", clear
	gen occ = _n
	tempfile pm 
	save `pm'
	
    use "E:\ProjData\PlacementCandidacy\utp-DataMainSample.dta", clear
	keep pEnklavVuxen 
	replace pEnklavVuxen = pEnklavVuxen*100
	gen occ = _n
	
	merge 1:1 occ using `pm'
	
    destring _all, replace
    replace hci = 2.25 if hci>2.25
	
	replace pden = pden * 100 
	drop if pden>4 & pden<.
	
	grstyle init
    grstyle set plain, horizontal 
    scatter lci pEnklavVuxen
	
	replace lci = lci+.5
	replace hci = hci+.5
	replace b = b+.5
	capture drop x2 x1
    twoway__histogram_gen pEnklavVuxen, width(.05) start(0) gen(x2 x1) 
    twoway bar x2 x1, barw(.1) 

	twoway (rarea lci hci pden, color(gs13))(line b pden, color(black)) (bar x2 x1 if inrange(x1, 0, 4) , barw(.05) color(gs13)), ///
	        graphregion(color(white)) ylabel(0 "-.5" .5 "0" 1 ".5" 1.5 "1" 2 "1.5" 2.5 "2" 3 "2.5", grid) legend(off) ///
			ytitle("Probability of nomination (%)") xtitle("Ethnic density (%)")
			
	graph export "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy\Output\predmarg1.png", as(png) replace
	
    gen where = -.5
	gen pipe = "|" 
	 
    twoway (rarea lci hci pden, color(gs13))(line b pden, color(black)) (scatter where pEnklavVuxen if inrange(pEnklavVuxen, 0, 3.7), mcolor(blue) ms(none) mlabel(pipe) mlabcolor(gs5%1) mlabpos(0) legend(off)), ///
	        graphregion(color(white)) ylabel(-0.5(.5)2.5) legend(off) ///
			ytitle("Probability of nomination (%)") xtitle("Ethnic density (%)") 
					
    graph export "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy\Output\predmarg2.png", as(png) replace    
exit
